﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TLSharp;

public partial class ChallengeProcess : System.Web.UI.Page
{
    public string CookieUserName = "";
    public string CookiePassword = "";
    public string AppGuid = System.Configuration.ConfigurationSettings.AppSettings["AppGuid"];

    protected void Page_Load(object sender, EventArgs e)
    {
        System.Text.StringBuilder ResponseText = new System.Text.StringBuilder();

        CookieUserName = Request.Cookies["TLUsername"] != null ? Request.Cookies["TLUsername"].Value : "";
        CookiePassword = Request.Cookies["TLPassword"] != null ? Request.Cookies["TLPassword"].Value : "";
        int TargetTeamID = (Request["TargetTeamID"]!=null?System.Convert.ToInt32(Request["TargetTeamID"]):0);
        int MinRank = ((Request["MinRank"]!=null && Request["MinRank"].Length!=0) ? System.Convert.ToInt32(Request["MinRank"]) : -1);
        int MaxRank = ((Request["MaxRank"] != null && Request["MaxRank"].Length != 0) ? System.Convert.ToInt32(Request["MaxRank"]) : System.Int32.MaxValue);
        bool bChallenge = System.Convert.ToBoolean(Request["bChallenge"] == "on");
        if (CookieUserName.Length > 0 && CookiePassword.Length > 0)
        {

            RequestMatchResponse MyReq = null;

            if (TargetTeamID > 0)
            {
                MyReq = TLSharp.TouchLeagueService.RequestMatchTargeted(CookieUserName, CookiePassword, AppGuid, TargetTeamID, bChallenge);
            }
            else
            {
                MyReq = TLSharp.TouchLeagueService.RequestMatchRank(CookieUserName, CookiePassword, AppGuid, MinRank, MaxRank, bChallenge);
            }
            //Check the Error
            if (MyReq.MessageType == "Error")
            {
                lblMessage.Text = MyReq.UserMessage;
            }
            else
            {
                if (MyReq.GameID > 0)
                {
                    GetTurnStateResponse TurnReq = TouchLeagueService.GetTurnState(CookieUserName, CookiePassword, AppGuid, MyReq.GameID);
                    //Todo: Start a new game here

                    //The following example shows how you can create a game and then set the state for the first turn
                    //MyGame NewMatch = new MyGame(TurnReq.Challenger, TurnReq.Challengee);
                    //string ScoreArray = NewMatch.Challengee.Value<string>("TeamID") + "," + NewMatch.GetScore(NewMatch.Challengee).ToString() + "|" + NewMatch.Challenger.Value<string>("TeamID") + "," + NewMatch.GetScore(NewMatch.Challenger).ToString();
                    //string MatchString = Server.UrlEncode(NewMatch.ToJSON());
                    //TouchLeagueService.SetTurnState(CookieUserName, CookiePassword, AppGuid, TurnReq.GameID, NewMatch.NextTurnTeamID, MatchString, ScoreArray, false);

                    string ReturnText = "";
                    ReturnText = "<ul id='GameCreated" + TurnReq.GameID.ToString() + "' title='GameCreated' selected='true'>";
                    ReturnText += "<li>A game has been created!  You have the first turn.</li>";
                    ReturnText += "<li>#" + TurnReq.Challenger.CurrentRank.ToString() + " " + TurnReq.Challenger.TeamGeo + " " + TurnReq.Challenger.TeamMascot + " vs. #" + TurnReq.Challengee.CurrentRank.ToString() + " " + TurnReq.Challengee.TeamGeo + " " + TurnReq.Challengee.TeamMascot + "</li>";
                    ReturnText += "<li><a href='Game.aspx?GameID=" + TurnReq.GameID.ToString() + "'>Play Your Turn</a></li>";
                    ReturnText += "</ul>";

                    Response.Write(ReturnText);
                    Response.End();


                }
                else
                {
                    ResponseText.Append("<ul><li>Your request has been submitted.  When the match starts you will find it under the 'My Games' option on the Home screen</li><li><a href='default.aspx' target=_self>Back Home</a></li></ul>");

                }
            }

        }
        else
        {
            ResponseText.Append("<ul><li>You have not set up your TouchLeague Credentials.  Do this on the home screen.</li><li><a href='default.aspx' target=_self>Back Home</a></li></ul>");

        }
        Response.Write(ResponseText.ToString());
        Response.End();
        
    }
}
